#!/bin/bash
function yum_repair() {
    echo "修改CentOS 6 YUM源" >>$logs_file
    whiptail --backtitle "Author: Elvis" --title "$prompt_message" --yesno "${yumRepair_prompt[0]}\
    \n${yumRepair_prompt[1]}\n1.${yumRepair_prompt[2]}\n2.${yumRepair_prompt[3]}\n\n${yumRepair_prompt[4]}？" 18 60 --yes-button "$goon_button" --no-button "$return_button"
    if [ "$?" != "0" ];then
        return 1
    fi
    
    base_yum_type=$(whiptail --backtitle "Author: Elvis" --title "BASE源选择" --radiolist --nocancel \
    "${yumRepair_prompt[5]}？" 12 60 2 \
    "1" "${yumRepair_prompt[6]}                                " ON \
    "2" "${yumRepair_prompt[7]}" OFF 3>&1 1>&2 2>&3)
    if [ "$?" != "0" ];then
        return 1
    else
        if [ "$base_yum_type" == "1" ];then
          base_yum="aliyun"
        elif [ "$base_yum_type" == "2" ];then
          base_yum="qinghua"
        fi
    fi
    
    epel_yum_type=$(whiptail --backtitle "Author: Elvis" --title "EPEL源选择" --radiolist --nocancel \
    "${yumRepair_prompt[8]}？" 12 60 2 \
    "1" "${yumRepair_prompt[9]}                                " ON \
    "2" "${yumRepair_prompt[10]}" OFF 3>&1 1>&2 2>&3)
    if [ "$?" != "0" ];then
        return 1
    else
        if [ "$epel_yum_type" == "1" ];then
          epel_yum="official"
        elif [ "$epel_yum_type" == "2" ];then
          epel_yum="chongqing"
        fi
    fi
    
    {
    ###备份
    #进度条输出格式echo -e "XXX\n进度数字\n输出内容\nXXX"
    msg="${yumRepair_process[0]}"
    echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
    echo -e "XXX\n1\n$msg\nXXX"
    echo 1
    sleep 1
    /bin/mv -f /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak >/dev/null 2>&1
    /bin/mv -f /etc/yum.repos.d/epel.repo /etc/yum.repos.d/epel.repo.bak >/dev/null 2>&1
    
    msg="${yumRepair_process[1]}"
    echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
    echo -e "XXX\n35\n$msg\nXXX"
    echo 35
    sleep 1
    if [ "$base_yum" == "aliyun" ];then
        cat >/etc/yum.repos.d/CentOS-Base.repo<<'EOF'
# CentOS-Base.repo
#
# The mirror system uses the connecting IP address of the client and the
# update status of each mirror to pick mirrors that are updated to and
# geographically close to the client.  You should use this for CentOS updates
# unless you are manually picking other mirrors.
#
# If the mirrorlist= does not work for you, as a fall back you can try the 
# remarked out baseurl= line instead.
#
#
 
[base]
name=CentOS-$releasever - Base - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos-vault/centos/$releasever/os/$basearch/
        http://mirrors.aliyuncs.com/centos-vault/centos/$releasever/os/$basearch/
        http://mirrors.cloud.aliyuncs.com/centos-vault/centos/$releasever/os/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos-vault/RPM-GPG-KEY-CentOS-6
 
#released updates 
[updates]
name=CentOS-$releasever - Updates - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos-vault/centos/$releasever/updates/$basearch/
        http://mirrors.aliyuncs.com/centos-vault/centos/$releasever/updates/$basearch/
        http://mirrors.cloud.aliyuncs.com/centos-vault/centos/$releasever/updates/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos-vault/RPM-GPG-KEY-CentOS-6
 
#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos-vault/centos/$releasever/extras/$basearch/
        http://mirrors.aliyuncs.com/centos-vault/centos/$releasever/extras/$basearch/
        http://mirrors.cloud.aliyuncs.com/centos-vault/centos/$releasever/extras/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos-vault/RPM-GPG-KEY-CentOS-6
 
#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever - Plus - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos-vault/centos/$releasever/centosplus/$basearch/
        http://mirrors.aliyuncs.com/centos-vault/centos/$releasever/centosplus/$basearch/
        http://mirrors.cloud.aliyuncs.com/centos-vault/centos/$releasever/centosplus/$basearch/
gpgcheck=1
enabled=0
gpgkey=http://mirrors.aliyun.com/centos-vault/RPM-GPG-KEY-CentOS-6
 
#contrib - packages by Centos Users
[contrib]
name=CentOS-$releasever - Contrib - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos-vault/centos/$releasever/contrib/$basearch/
        http://mirrors.aliyuncs.com/centos-vault/centos/$releasever/contrib/$basearch/
        http://mirrors.cloud.aliyuncs.com/centos-vault/centos/$releasever/contrib/$basearch/
gpgcheck=1
enabled=0
gpgkey=http://mirrors.aliyun.com/centos-vault/RPM-GPG-KEY-CentOS-6
EOF
    else
        cat >/etc/yum.repos.d/CentOS-Base.repo<<'EOF'
# CentOS-Base.repo
#
# The mirror system uses the connecting IP address of the client and the
# update status of each mirror to pick mirrors that are updated to and
# geographically close to the client.  You should use this for CentOS updates
# unless you are manually picking other mirrors.
#
# If the mirrorlist= does not work for you, as a fall back you can try the 
# remarked out baseurl= line instead.
#
#

[base]
name=CentOS-$releasever - Base
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os&infra=$infra
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos-vault/centos/$releasever/os/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6

#released updates 
[updates]
name=CentOS-$releasever - Updates
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates&infra=$infra
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos-vault/centos/$releasever/updates/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6

#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras&infra=$infra
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos-vault/centos/$releasever/extras/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6

#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever - Plus
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus&infra=$infra
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos-vault/centos/$releasever/centosplus/$basearch/
gpgcheck=1
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6

#contrib - packages by Centos Users
[contrib]
name=CentOS-$releasever - Contrib
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=contrib&infra=$infra
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos-vault/centos/$releasever/contrib/$basearch/
gpgcheck=1
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
EOF
    fi
    
    msg="${yumRepair_process[2]}"
    echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
    echo -e "XXX\n85\n$msg\nXXX"
    echo 85
    sleep 1
    if [ "$epel_yum" == "official" ];then
        cat > /etc/yum.repos.d/epel.repo <<'EOF'
[epel]
name=Extra Packages for Enterprise Linux 6 - $basearch
baseurl=http://archives.fedoraproject.org/pub/archive/epel/6/$basearch
failovermethod=priority
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6
 
[epel-debuginfo]
name=Extra Packages for Enterprise Linux 6 - $basearch - Debug
baseurl=http://archives.fedoraproject.org/pub/archive/epel/6/$basearch/debug
failovermethod=priority
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6
gpgcheck=0
 
[epel-source]
name=Extra Packages for Enterprise Linux 6 - $basearch - Source
baseurl=http://archives.fedoraproject.org/pub/archive/epel/6/SRPMS
failovermethod=priority
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6
gpgcheck=0
EOF
    else
        cat > /etc/yum.repos.d/epel.repo <<'EOF'
[epel]
name=Extra Packages for Enterprise Linux 6 - $basearch
baseurl=https://mirrors.cqu.edu.cn/epel/6/$basearch
failovermethod=priority
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6
 
[epel-debuginfo]
name=Extra Packages for Enterprise Linux 6 - $basearch - Debug
baseurl=https://mirrors.cqu.edu.cn/epel/6/$basearch/debug
failovermethod=priority
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6
gpgcheck=0
 
[epel-source]
name=Extra Packages for Enterprise Linux 6 - $basearch - Source
baseurl=https://mirrors.cqu.edu.cn/epel/6/SRPMS
failovermethod=priority
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6
gpgcheck=0
EOF
    fi
    
    echo -e "XXX\n100\nDone！！！\nXXX"
    echo 100
    sleep 1.30
    echo "">>$logs_file
    echo "">>$logs_file
    
    } | whiptail --backtitle "Author: Elvis" --title "${yumRepair_process[3]}" --gauge " " 6 60 0

    whiptail --backtitle "Author: Elvis" --title "$prompt_message" --msgbox "${yumRepair_process[4]}！！！" 10 70

}